package com.game.data;

import com.game.config.Coefficient;
import com.game.model.BaseProperty;
import com.game.model.SpecialProperty;
import com.game.model.api.Property;
import com.game.utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:属性数据
 **/
public class PropertyData {
    public static Property getBasePropertyById(int id) {
        List<BaseProperty> baseProperties = baseProperties();
        for (BaseProperty b : baseProperties) {
            if (b.getId() == id) {
                return b;
            }
        }
        return null;
    }

    public static Property getSpecialPropertyById(int id) {
        List<SpecialProperty> specialProperties = specialProperties();
        for (SpecialProperty s : specialProperties) {
            if (s.getId() == id) {
                return s;
            }
        }
        return null;
    }

    public static List<BaseProperty> baseProperties() {
        ArrayList<BaseProperty> baseProperties = new ArrayList<>();
        //使用当前数据
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            baseProperties.add(new BaseProperty(1, 12000, 100, 100, 100));
            baseProperties.add(new BaseProperty(2, 20000, 100, 100, 100));
            baseProperties.add(new BaseProperty(3, 1000, 100, 100, 100));
            baseProperties.add(new BaseProperty(4, 10000, 100, 100, 100));
        }
        //使用数据库数据
        if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from base_property");
                while (rs.next()) {
                    BaseProperty baseProperty = new BaseProperty();
                    baseProperty.setId(rs.getInt("id"));
                    baseProperty.setHP(rs.getLong("hp"));
                    baseProperty.setMp(rs.getLong("mp"));
                    baseProperty.setATK(rs.getLong("atk"));
                    baseProperty.setDEF(rs.getLong("def"));
                    baseProperties.add(baseProperty);
                }
                JDBCUtil.close(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return baseProperties;
    }

    public static List<SpecialProperty> specialProperties() {
        ArrayList<SpecialProperty> specialProperties = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            specialProperties.add(new SpecialProperty(111, 10, 10, 10, 10));
            specialProperties.add(new SpecialProperty(112, 10, 10, 10, 10));
        }
        else if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from special_property");
                while (rs.next()) {
                    SpecialProperty specialProperty = new SpecialProperty();
                    specialProperty.setId(rs.getInt("id"));
                    specialProperty.setHIT(rs.getInt("hit"));
                    specialProperty.setAVO(rs.getInt("avo"));
                    specialProperty.setCRI(rs.getInt("cri"));
                    specialProperty.setCHR(rs.getInt("chr"));
                    specialProperties.add(specialProperty);
                }
                JDBCUtil.close(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return specialProperties;
    }
}
